package com.baixiaowen.xiaoaointerview.Java编程功底篇.手写栈_队列相关算法;

/**
 *
 * 括号匹配问题
 *
 * @author Baixiaowen
 */
public class Bracket {

    public static boolean isMatch(String str) {
        Stack<Character> stack = new Stack<>();

        for (char c : str.toCharArray()) {
            if (c == '{') {
                stack.push(c);
                continue;
            }
            if (c == '}') {
                if (stack.size() == 0 ) {
                    return false;
                }
                char item = stack.pop();
                if (item != '{') {
                    return false;
                }
            }
        }

        return stack.size() == 0;
    }

    public static void main(String[] args) {
        String[] cases = new String[]{
                "{1}{2}{3}",
                "{{1+2}{{3+4}}}",
                "1*{2+3*{4+5}}",
                "",
                "{",
                "}}{{",
                "{{{}",
        };

        Boolean[] values = new Boolean[] {
                true,
                true,
                true,
                true,
                false,
                false,
                false
        };

        for(int i = 0; i < cases.length; i++) {
            String c = cases[i];
            if (values[i] == isMatch(c)) {
                System.err.println("成功");
            }
        }
    }

}
